package com.loveyukari.support;

import java.io.Serializable;

import com.loveyukari.common.Constant;


/**
 * @author chenyu
 *
 */
public class Result<T> implements Serializable {
	private static final long serialVersionUID = 1L;

	private int code = 0;
	private String message = null;
	private T data = null;
	
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Result [code=");
		builder.append(code);
		builder.append(", ");
		if (message != null) {
			builder.append("message=");
			builder.append(message);
			builder.append(", ");
		}
		if (data != null) {
			builder.append("data=");
			builder.append(data);
		}
		builder.append("]");
		return builder.toString();
	}
	
	public static final Result<Object> error(String message){
		Result<Object> r = new Result<Object>();
		r.setMessage(message);
		r.setCode(Constant.ERROR_CODE);
		return r;
	}
	
	public static final Result<Object> success(String message){
		Result<Object> r = new Result<Object>();
		r.setMessage(message);
		r.setCode(Constant.SUCCESS_CODE);
		return r;
	}
	
	public static final <E> Result<E> success(String message, E data){
		Result<E> r = new Result<E>();
		r.setMessage(message);
		r.setCode(Constant.SUCCESS_CODE);
		r.setData(data);
		return r;
	}
	
}
